from PIL import Image
import numpy as np
from wordcloud import WordCloud
import os


def create_word_cloud(keyword_dict, file_name):
    keyword_list = []
    for keyword, frequency in keyword_dict.items():
        keyword_list.extend([keyword] * frequency)

    if len(keyword_list) == 0:
        keyword_list = ["空"]
    rabit_mask = np.array(Image.open(os.path.join(os.path.dirname(__file__), "rabit.png")))

    wordcloud = WordCloud(background_color="white", max_words=2000, mask=rabit_mask,
                          font_path=os.path.join(os.path.dirname(__file__), "simhei.ttf"),
                          contour_width=15, contour_color='red', random_state=66, width=400, height=600,
                          scale=3).generate(' '.join(keyword_list))
    wordcloud.to_file(file_name)
